import { getCookie } from '@/utils/cookie'
import axios from 'axios'
// 环境的切换
if (process.env.NODE_ENV === 'development') {
  axios.defaults.baseURL = 'http://localhost:8866/ruan' // 开发环境
} else if (process.env.NODE_ENV === 'debug') {
} else if (process.env.NODE_ENV === 'production') {
axios.defaults.baseURL = 'http://39.105.199.252:8866/ruan' // 开发环境
}
export const baseURL = "http://localhost:8866/ruan"
export function get(url: string, data: any, method: string) {
  return new Promise((resolve, reject) => {
    axios[method](url, { params: data })
      .then((res: any) => {
        resolve(res)
      })
      .catch((err: any) => {
        reject(err)
      })
  })
} 
export function post(url: string, data: any, method: string) {
  return new Promise((resolve, reject) => {
     axios[method](url, data)
      .then((res: any) => {
        resolve(res)
      })
      .catch((err: any) => {
        reject(err)
      })
  })
}
//使用axios.post要设置此响应头，否则后台无法获取数据
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data';
axios.defaults.headers.Authorization = getCookie("token");
// 允许当前axios携带cookie
axios.defaults.withCredentials = true

// 请求拦截
axios.interceptors.request.use(config => {
  //每次请求时可添加加载logo
  config.headers.Authorization = getCookie("token");
   return config
}, error => {
  console.log("请求失败");
})

// 响应拦截
axios.interceptors.response.use(config => {
  //直接返回数据
  return config.data
}, error => {
  //弹出错误窗口
  console.log("响应失败");
})
